package com.basker.pisces.redis.concurrent.collection;

import java.util.Set;

import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import com.basker.pisces.concurrent.collection.AbstractConcurrentCollectionManager;
import com.basker.pisces.redis.utils.RedisUtils;

public class RedissonCollectionManager extends AbstractConcurrentCollectionManager {

    private static String SET_PREFIX = "set";

    @Autowired(required = false)
    private RedissonClient redissonClient;

    public RedissonCollectionManager(String name) {
        super(name);
    }

    public RedissonCollectionManager(String name, RedissonClient redissonClient) {
        super(name);
        this.redissonClient = redissonClient;
    }

    @Override
    protected Set<String> createSet(String name) {
        Assert.notNull(this.redissonClient, "property 'redissonClient' is required");
        return redissonClient.getSet(this.resolveKey(name));
    }

    private String resolveKey(String name) {
        return RedisUtils.resolveKeyWithApplicationName(String.join(":", SET_PREFIX, name));
    }

}
